package project.web;

import Utils.web.servlet.BaseServlet;
import project.pojo.Cart;
import project.pojo.User;
import project.service.OrderService;
import project.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author d11yu
 */
@WebServlet(name = "OrderServlet", value = "/OrderServlet")
public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OrderServiceImpl();

    protected void createOrder(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            // 如果未登录
            response.sendRedirect(request.getContextPath() + "/pages/user/login.jsp");
        } else {
            String orderId  = orderService.createOrder(cart, user.getId());
            request.getSession().setAttribute("orderId", orderId);
            response.sendRedirect(request.getContextPath() + "/pages/cart/checkout.jsp");
        }
    }
}
